#include "deshow.h"
// RGB565 conversion
// RGB565 is R(5)+G(6)+B(5)=16bit color format.
// Bit image "RRRRRGGGGGGBBBBB"
uint16_t rgb565_conv(uint16_t r,uint16_t g,uint16_t b) {
	return (((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3));
}
uint8_t JPEGshow(char * file, int width, int height,int _cols,int _rows) {
	int32_t startTick, endTick, diffTick;
	startTick = xTaskGetTickCount();
	pixel_s **pixels;
	uint16_t imageWidth;//图片宽度
	uint16_t imageHeight;//图片高度
	esp_err_t err = decode_image(&pixels, file, width, height, &imageWidth, &imageHeight);
	ESP_LOGI(__FUNCTION__, "decode_image err=%d imageWidth=%d imageHeight=%d", err, imageWidth, imageHeight);
	if (err == ESP_OK) {

		uint16_t _width = width;//135
		uint16_t _height = height;
		uint16_t *colors = (uint16_t*)malloc(sizeof(uint16_t) * _width);



		for(int y = 0; y < _height; y++){
			for(int x = 0;x < _width; x++){
				pixel_s pixel = pixels[y][x];
				colors[x] = rgb565_conv(pixel.red, pixel.green, pixel.blue);
			}
		/*

		此处放

		*/
			//lcdDrawMultiPixels(dev, _cols, y+_rows, _width, colors);
			//vTaskDelay(1);
		}

		free(colors);
		release_image(&pixels, width, height);
		ESP_LOGD(__FUNCTION__, "Finish");
	}

	endTick = xTaskGetTickCount();
	diffTick = endTick - startTick;
	ESP_LOGI(__FUNCTION__, "elapsed time[ms]:%d",diffTick*portTICK_RATE_MS);
	return diffTick;
}
uint8_t PNG_SHOW(char * file, int width, int height,uint8_t *data,uint8_t w) {


		FILE* fp = fopen(file, "rb");
		if (fp == NULL) {
				ESP_LOGW(__FUNCTION__, "File not found [%s]", file);
				return 0;
		}
		char buf[1024];
		size_t remain = 0;
		int len;
		pngle_t *pngle = pngle_new(width, height);

		pngle_set_init_callback(pngle, png_init);
		pngle_set_draw_callback(pngle, png_draw);
		pngle_set_done_callback(pngle, png_finish);

		double display_gamma = 2.2;
		pngle_set_display_gamma(pngle, display_gamma);

		while (!feof(fp)) {
				if (remain >= sizeof(buf)) {
						ESP_LOGE(__FUNCTION__, "Buffer exceeded");
						while(1) vTaskDelay(1);
				}

				len = fread(buf + remain, 1, sizeof(buf) - remain, fp);
				if (len <= 0) {
						//printf("EOF\n");
						break;
				}

				int fed = pngle_feed(pngle, buf, remain + len);
				if (fed < 0) {
						ESP_LOGE(__FUNCTION__, "ERROR; %s", pngle_error(pngle));
						while(1) vTaskDelay(1);
				}

				remain = remain + len - fed;
				if (remain > 0) memmove(buf, buf + fed, remain);
		}

		fclose(fp);

		uint16_t _width = width;
	
		uint16_t _height = height;
	
		for(int y = 0; y < _height; y++){
				for(int x = 0;x < _width; x++){
						pixel_png pixel = pngle->pixels[y][x];
						uint16_t color = rgb565_conv(pixel.red, pixel.green, pixel.blue);
								// if(color!=BLACK)
								// {
								*(data+y*2*w+x*2) = (color >> 8) & 0xFF;
								*(data+y*2*w+x*2+1)= color & 0xFF;
								// }

				}
		}

		pngle_destroy(pngle, width, height);
		return 0;
}
void png_init(pngle_t *pngle, uint32_t w, uint32_t h)
{
		ESP_LOGD(__FUNCTION__, "png_init w=%d h=%d", w, h);
		ESP_LOGD(__FUNCTION__, "screenWidth=%d screenHeight=%d", pngle->screenWidth, pngle->screenHeight);
		pngle->imageWidth = w;
		pngle->imageHeight = h;
		pngle->reduction = false;
		pngle->scale_factor = 1.0;

		// Calculate Reduction
		if (pngle->screenWidth < pngle->imageWidth || pngle->screenHeight < pngle->imageHeight) {
				pngle->reduction = true;
				double factorWidth = (double)pngle->screenWidth / (double)pngle->imageWidth;
				double factorHeight = (double)pngle->screenHeight / (double)pngle->imageHeight;
				pngle->scale_factor = factorWidth;
				if (factorHeight < factorWidth) pngle->scale_factor = factorHeight;
				pngle->imageWidth = pngle->imageWidth * pngle->scale_factor;
				pngle->imageHeight = pngle->imageHeight * pngle->scale_factor;
		}
		ESP_LOGD(__FUNCTION__, "reduction=%d scale_factor=%f", pngle->reduction, pngle->scale_factor);
		ESP_LOGD(__FUNCTION__, "imageWidth=%d imageHeight=%d", pngle->imageWidth, pngle->imageHeight);

}

void png_draw(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t rgba[4])
{
		ESP_LOGD(__FUNCTION__, "png_draw x=%d y=%d w=%d h=%d", x,y,w,h);
#if 0
		uint8_t r = rgba[0];
		uint8_t g = rgba[1];
		uint8_t b = rgba[2];
#endif

		// image reduction
		uint32_t _x = x;
		uint32_t _y = y;
		if (pngle->reduction) {
				_x = x * pngle->scale_factor;
				_y = y * pngle->scale_factor;
		}
		if (_y < pngle->screenHeight && _x < pngle->screenWidth) {
				pngle->pixels[_y][_x].red = rgba[0];
				pngle->pixels[_y][_x].green = rgba[1];
				pngle->pixels[_y][_x].blue = rgba[2];
		}

}
void png_finish(pngle_t *pngle) {
		ESP_LOGD(__FUNCTION__, "png_finish");
}





